package com.ruoyi.web.controller.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.enums.YesNoEnum;
import com.ruoyi.stu.enums.StuStudentCourseEnum;
import com.ruoyi.system.domain.SysStudentCourse;
import com.ruoyi.system.enums.SysStudentCourseEnum;
import com.ruoyi.system.mapper.SysStudentCourseMapper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Example;
import io.swagger.annotations.ExampleProperty;

@Api(tags = "课程管理接口")
@RestController
@RequestMapping("/api/v1/courses")
public class ApiCoursesController extends BaseController {
    @Autowired
    private SysStudentCourseMapper sysStudentCourseMapper;

    @ApiOperation(
        value = "获取用户已注册课程列表", 
        notes = "获取当前登录用户的所有已注册和已完成的课程列表",
        response = AjaxResult.class
    )
    @ApiImplicitParams({
        @ApiImplicitParam(
            name = "Authorization",
            value = "Bearer Token认证信息",
            required = true,
            dataType = "String",
            paramType = "header",
            example = "Bearer eyJhbGciOiJIUzUxMiJ9.xxx"
        )
    })
    @ApiResponses({
        @ApiResponse(
            code = 200, 
            message = "操作成功", 
            response = AjaxResult.class,
            examples = @Example(value = {
                @ExampleProperty(
                    mediaType = "application/json", 
                    value = "{\"code\":200,\"msg\":\"操作成功\",\"data\":{\"registered\":[{\"courseId\":\"course_001\",\"courseName\":\"初中英语词汇进阶\",\"status\":\"registered\"}],\"completed\":[{\"courseId\":\"course_002\",\"courseName\":\"小学英语基础词汇\",\"status\":\"completed\"}]}}"
                )
            })
        ),
        @ApiResponse(
            code = 401,
            message = "未登录或Token已过期"
        ),
        @ApiResponse(
            code = 500, 
            message = "系统内部错误"
        )
    })
    @GetMapping("/my-courses")
    public AjaxResult myCourses() {
        // 获取当前登录用户信息
        Long studentId = SecurityUtils.getLoginStudentId();
        List<SysStudentCourse> listSysStudentCourse = sysStudentCourseMapper.listByStudentId(studentId, null);
        
        // 这里先返回模拟数据
        Map<String, List<Map<String, Object>>> result = new HashMap<>();
        
        // 已注册课程列表
        List<Map<String, Object>> registered = new ArrayList<>();
        // 已完成课程列表
        List<Map<String, Object>> completed = new ArrayList<>();
        
        for(SysStudentCourse e : listSysStudentCourse) {
        	Integer stateFinished = e.getStateFinished();
            Map<String, Object> courseMap = new HashMap<>();
        	if (stateFinished == YesNoEnum.YES.value) {
                courseMap.put("courseId", e.getCourseId());
                courseMap.put("courseName", e.getCourseName());
                courseMap.put("status", "completed");
                completed.add(courseMap);
			}

        	{
                courseMap.put("courseId", e.getCourseId());
                courseMap.put("courseName", e.getCourseName());
                courseMap.put("status", "registered");
                registered.add(courseMap);
        	}
        }
        
        result.put("registered", registered);
        result.put("completed", completed);
        
        return AjaxResult.success(result);
    }
    
    // 在单词表里面获取课程
    @GetMapping("/listCourseForDanCiBiao")
    public AjaxResult listCourseForDanCiBiao() {
        // 获取当前登录用户信息
        Long studentId = SecurityUtils.getLoginStudentId();
        List<SysStudentCourse> listSysStudentCourse = sysStudentCourseMapper.listByStudentId(studentId, null);
        
        // 这里先返回模拟数据
        Map<String, List<Map<String, Object>>> result = new HashMap<>();
        
        // 已注册课程列表
        List<Map<String, Object>> courses = new ArrayList<>();
        
        for(SysStudentCourse e : listSysStudentCourse) {
        	Integer stateFinished = e.getStateFinished();
            Map<String, Object> courseMap = new HashMap<>();
            
            courseMap.put("courseId", e.getCourseId());
            courseMap.put("courseName", e.getCourseName());
            courseMap.put("status", StuStudentCourseEnum.Complete.getFrontValue(stateFinished));
            courses.add(courseMap);
        }
        
        result.put("courses", courses);
        
        return AjaxResult.success(result);
    }
}